package com.view.jameson.androidrecyclerviewcard.hv2;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.view.jameson.androidrecyclerviewcard.BuildConfig;
import com.view.jameson.androidrecyclerviewcard.CardViewBean;
import com.view.jameson.androidrecyclerviewcard.R;

import java.util.List;
import java.util.Random;


/**
 * Created by chensuilun on 2016/11/15.
 */
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.ViewHolder> implements View.OnClickListener {
    public static final int VIEW_TYPE_IMAGE = 0;
    public static final int VIEW_TYPE_TEXT = 1;
    private static final String TAG = "DemoAdapter";
    private List<CardViewBean> mItems;
    private int mType = VIEW_TYPE_IMAGE;
    private OnItemClickListener mOnItemClickListener;
    private Context mContext;

    public DemoAdapter(Context mContext,List<CardViewBean> items) {
        this(items, VIEW_TYPE_IMAGE);
        this.mContext = mContext;
    }

    public DemoAdapter(List<CardViewBean> items, int type) {
        this.mItems = items;
        mType = type;
    }

//    public void addData() {
//        if (mItems != null) {
//            for (int i = 0; i < 10; i++) {
//                mItems.add("Extra:" + i);
//            }
//            notifyDataSetChanged();
//        }
//    }

    private static final Random RANDOM = new Random();

//    public int dataChange() {
//        int result = 0;
//        if (mItems != null) {
//            if (RANDOM.nextBoolean()) {
//                for (int i = 0; i < 10; i++) {
//                    mItems.add("Extra:" + i);
//                }
//                result = 1;
//            } else {
//                int size = mItems.size();
//                int cut = size / 2;
//                for (int i = size - 1; i > cut; i--) {
//                    mItems.remove(i);
//                }
//                result = -1;
//            }
//            notifyDataSetChanged();
//        }
//        return result;
//    }

    public DemoAdapter setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
        return this;
    }

    @Override
    public int getItemViewType(int position) {
        return mType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if (BuildConfig.DEBUG) {
//            Log.e(TAG, "onCreateViewHolder: type:" + viewType);
//        }


        View v;
//        if (viewType == VIEW_TYPE_IMAGE) {
        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_demo, parent, false);
//        } else {
//            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_tv, parent, false);
//        }
        v.setOnClickListener(this);


        // 自定义view的宽度,控制一屏显示的个数
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        params.width = width / 4;   //这里每屏显示3个<将屏幕平均分为5份
        v.setLayoutParams(params);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
//        if (BuildConfig.DEBUG) {
//            Log.d(TAG, "onBindViewHolder: position:" + position);
//        }
//        String item = mItems.get(position % mItems.size());
//        if (mType == VIEW_TYPE_TEXT) {
//            holder.text.setText(item);
//        } else {
//            holder.text.setText("HelloWorld：" + item);
//        }

        holder.itemView.setTag(position);
        ImageView imageView = holder.imageView;
//        RelativeLayout cardAl = holder.cardAl;
        Glide.with(imageView.getContext())
                .load(mItems.get(position % mItems.size()).getPicturePath2())
//                    .placeholder(R.mipmap.ic_launcher)
                .into(imageView);
//        if (mItems.get(position % mItems.size()).isSelected()) {
//            cardAl.setBackground(cardAl.getContext().getDrawable(R.drawable.shape_f80069_storke_2_r8));
//        } else {
//            cardAl.setBackground(cardAl.getContext().getDrawable(R.drawable.shape_a1a0a5_storke_1_r8));
//        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(imageView.getContext(), position % mItems.size() + "==" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
//        return mItems.size();
        return Integer.MAX_VALUE;
    }

    @Override
    public void onClick(final View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    /**
     * @author chensuilun
     */
    protected static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView text;
        public ImageView imageView;
//        public RelativeLayout cardAl;

        public ViewHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.item_tv_title);
            imageView = itemView.findViewById(R.id.imageView);
//            cardAl = itemView.findViewById(R.id.cardAl);
        }
    }

    /**
     * @author chensuilun
     */
    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }
}
